package com.smedi.ismedi.constructionproject.core.port.adapter.service.impl;

import com.smedi.ismedi.constructionproject.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.constructionproject.core.port.adapter.service.OrganizationRemoteApiService;
import com.smedi.ismedi.organization.api.dto.response.HrOrganizationResponse;
import com.smedi.ismedi.organization.api.dto.response.ProjectOrganizationResponse;
import com.smedi.ismedi.organization.api.feign.RemoteHrOrganizationFeignApi;
import com.smedi.ismedi.organization.api.feign.RemoteProjectOrganizationFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-21 11:15
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrganizationRemoteApiServiceImpl implements OrganizationRemoteApiService {
    private final RemoteHrOrganizationFeignApi remoteHrOrganizationFeignApi;

    private final RemoteProjectOrganizationFeignApi remoteProjectOrganizationFeignApi;

    @Override
    public HrOrganizationResponse getHrOrganizationRoot() {
        ResponseData<HrOrganizationResponse> response = remoteHrOrganizationFeignApi.getHrOrganizationRoot();
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public Boolean checkHrOrganizationCanHasProjectAvailable(Long hrOrganizationId) {
        ResponseData<Boolean> response = remoteHrOrganizationFeignApi.getCheckHrOrganizationCanHasProjectAvailable(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public Boolean checkProjectOrganizationAvailable(Long projectOrganizationId) {
        ResponseData<Boolean> response = remoteProjectOrganizationFeignApi.getCheckAvailable(projectOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public HrOrganizationResponse getHrOrganizationView(Long hrOrganizationId) {
        ResponseData<HrOrganizationResponse> response = remoteHrOrganizationFeignApi.getHrOrganizationView(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }
    @Override
    public List<Long> getProjectIdsByHrOrganizationId(Long hrOrganizationId) {
        ResponseData<List<Long>> response = remoteHrOrganizationFeignApi.getHrOrganizationChildrenOrgIdList(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }


    @Override
    public ProjectOrganizationResponse getProjectOrganizationView(Long projectOrganizationId) {
        ResponseData<ProjectOrganizationResponse> response = remoteProjectOrganizationFeignApi.getProjectOrganizationView(projectOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public Boolean checkProjectOrganizationCanBeBidSectionConfig(Long projectOrganizationId) {
        ResponseData<Boolean> response = remoteProjectOrganizationFeignApi.getCheckProjectOrganizationCanBeConfigOrganization(projectOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public Boolean checkHrOrganizationAvailable(Long hrOrganizationId) {
        ResponseData<Boolean> response = remoteHrOrganizationFeignApi.getCheckAvailable(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<Long> getHrOrganizationChildrenOrgIdList(Long hrOrganizationId) {
        ResponseData<List<Long>> response = remoteHrOrganizationFeignApi.getHrOrganizationChildrenOrgIdList(hrOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public Boolean checkProjectOrganizationIsExternalUnit(Long projectOrganizationId) {
        ResponseData<Boolean> response = remoteProjectOrganizationFeignApi.getCheckProjectOrganizationIsExternalUnit(projectOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<Long> getProjectOrganizationChildrenOrgIdList(Long projectOrganizationId) {
        ResponseData<List<Long>> response = remoteProjectOrganizationFeignApi.getProjectOrganizationChildrenProjectOrganizationIdList(projectOrganizationId);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<ProjectOrganizationResponse> getProjectOrganizationByProjectOrganizationIds(List<Long> projectOrganizationIds) {
        ResponseData<List<ProjectOrganizationResponse>> response = remoteProjectOrganizationFeignApi.getQueryProjectOrganizationList(projectOrganizationIds);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }

    @Override
    public List<HrOrganizationResponse> getHrOrganizationByHrOrganizationIds(List<Long> hrOrganizationIds) {
        ResponseData<List<HrOrganizationResponse>> response = remoteHrOrganizationFeignApi.getQueryHrOrganizationList(hrOrganizationIds);
        if (response.isSuccess()) {
            return response.getData();
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, response.getApplication(), String.valueOf(response.getErrorMsg()));
        }
    }
}
